<template>
  <!-- visible控制显示和隐藏 -->
  <el-dialog :title="title = form.id ? '查看部门' : '新增部门'" :visible="showDialog" @close="close">
    <el-form ref="AddForm" :rules="rules" :model="form">
      <el-form-item label="部门名称" :label-width="formLabelWidth" prop="name">
        <el-input v-model="form.name" clearable autocomplete="off" />
      </el-form-item>
      <el-form-item label="部门编码" :label-width="formLabelWidth" prop="code">
        <el-input v-model="form.code" clearable autocomplete="off" />
      </el-form-item>
      <el-form-item label="部门负责人" :label-width="formLabelWidth" prop="manager">
        <el-select v-model="form.manager" clearable placeholder="请选择部门负责人" @focus="getEmpList">
          <el-option v-for="item in empList" :key="item.id" :label="item.username" :value="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" :label-width="formLabelWidth" prop="introduce">
        <el-input v-model="form.introduce" type="textarea" clearable :rows="3" placeholder="请输入部门介绍" resize="none" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getDepts, addDepts, deptsDetail, editDepts } from '@/api/departments'
import { getEmpSimple } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      required: true
    },
    pid: {
      type: String,
      required: true
    }
  },
  data() {
    // 自定义规则
    const checkName = async(rule, value, callback) => {
      const res = await getDepts()
      if (this.form.id) {
        const list = res.data.depts.filter(v => v.pid === this.form.pid && v.name !== this.oldForm.name)
        const result = list.find((v) => v.name === value)
        result ? callback(new Error('部门名称已存在')) : callback()
      } else {
        const children = res.data.depts.filter(v => v.pid === this.pid)
        const result = children.find(v => v.name === value)
        result ? callback(new Error('部门名称已存在')) : callback()
      }
    }
    const checkCode = async(rule, value, callback) => {
      if (this.form.id && this.oldForm.code === value) callback()
      const res = await getDepts()
      const result = res.data.depts.find((v) => v.code === value)
      result ? callback(new Error('部门编码已存在')) : callback()
    }
    return {
      // 左侧文字宽度
      formLabelWidth: '100px',
      // 跟表单绑定的对象
      form: {},
      oldForm: {},
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 50, message: '名称在1-50之间', trigger: 'blur' },
          { validator: checkName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 1, max: 50, message: '编码在1-50之间', trigger: 'blur' },
          { validator: checkCode, trigger: 'blur' }
        ],

        manager: [
          { required: true, message: '请选择负责人', trigger: 'change' }
        ],

        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          { min: 1, max: 300, message: '编码在1-300之间', trigger: 'blur' }
        ]
      },
      empList: []
    }
  },
  methods: {
    close() {
      this.form = {}
      this.$refs.AddForm.resetFields()
      this.$emit('update:showDialog', false)
    },
    async getEmpList() {
      const res = await getEmpSimple()
      this.empList = res.data
    },
    save() {
      this.$refs.AddForm.validate(async v => {
        if (v) {
          this.form.id ? await editDepts(this.form) : await addDepts({ ...this.form, pid: this.pid })
          this.close()
          this.$emit('reload')
        }
      })
    },
    async getDetail(id) {
      const res = await deptsDetail(id)
      this.form = res.data
      this.oldForm = this.form.name
      this.oldForm = this.form.code
    }
  }
}
</script>

<style lang="scss" scoped>
.el-dialog__wrapper {
  .el-dialog {
    .el-dialog__body {
      .el-form {
        .el-form-item:nth-child(3) {
          .el-form-item__content {
            .el-select {
              width: 100%;
            }
          }
        }
      }
    }
  }
}
</style>
